package com.yingpai.fitness.dvp_2_0.main.activity.dynamic;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.orhanobut.logger.Logger;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.yingpai.fitness.R;
import com.yingpai.fitness.dvp_2_0.common.activity.CommonActivity;
import com.yingpai.fitness.dvp_2_0.common.adapter.BaseAdapterHelper;
import com.yingpai.fitness.dvp_2_0.common.adapter.QuickAdapter;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.main.widget.staggered.StaggeredGridView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ASUS on 2019/2/28.
 */

public class Dvp_2_1_PersonalDetailActivity extends CommonActivity implements OnRefreshLoadMoreListener {

    @BindView(R.id.staggered)
    StaggeredGridView staggered;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.dynamic_tv_top)
    TextView dynamicTvTop;
    @BindView(R.id.dynamic_line_top)
    View dynamicLineTop;
    @BindView(R.id.shop_tv_top)
    TextView shopTvTop;
    @BindView(R.id.shop_line_top)
    View shopLineTop;
    @BindView(R.id.top_ll_1)
    LinearLayout topLl1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusFlag(Contacts.NORMAL_STATUS);
        setContentView(R.layout.dvp_2_1_activity_personal_detail);
    }

    @Override
    protected void initView() {
        refreshLayout.setOnRefreshLoadMoreListener(this);
        refreshLayout.setRefreshHeader(new MaterialHeader(context).setColorSchemeResources(R.color.common_color));

        final View headview = View.inflate(context, R.layout.personal_detail_head_view, null);

        staggered.addHeaderView(headview);

        QuickAdapter<String> quickAdapter = new QuickAdapter<String>(context, R.layout.item_hot_dynamic_list) {
            @Override
            protected void convert(BaseAdapterHelper helper, String item) {
                ImageView imageView = helper.getView(R.id.photo);
                if (helper.getPosition() == 0) {
                    helper.setText(R.id.title, "导入之后不能用啊，StaggeredGridView cannot be resolved to a type StaggeredGridView不能定义变量啊，求解导入之后不能用啊，StaggeredGridView cannot be resolved");
                } else if (helper.getPosition() == 3) {
                    helper.setText(R.id.title, "导入之后不能用啊，StaggeredGridView cannot be resolved to a type StaggeredGridView");
                } else if (helper.getPosition() == 4) {
                    Glide.with(context)
                            .load("http://c.hiphotos.baidu.com/zhidao/pic/item/562c11dfa9ec8a1359aa88b6f103918fa0ecc030.jpg")
                            .into(imageView);
                } else {

                }
            }
        };
        staggered.setAdapter(quickAdapter);

        staggered.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                Logger.e("onScroll" + firstVisibleItem);

                if (Math.abs(headview.getTop()) >= headview.findViewById(R.id.top_ll).getHeight() - topLl1.getHeight()) {
                    topLl1.setVisibility(View.VISIBLE);
                } else {
                    topLl1.setVisibility(View.INVISIBLE);
                }

                if ((visibleItemCount > 0) && (firstVisibleItem == 0)) {
                    if (view.getChildAt(0).getTop() >= 0) {
                        refreshLayout.setEnableRefresh(true);
                    } else {
                        refreshLayout.setEnableRefresh(false);
                    }
                } else {
                    refreshLayout.setEnableRefresh(false);
                }

                if ((totalItemCount > 0) && (view.getLastVisiblePosition() == totalItemCount - 1)) {
                    if (view.getBottom() >= view.getChildAt(view.getChildCount() - 1).getBottom()) {
                        refreshLayout.setEnableLoadMore(true);
                    } else {
                        refreshLayout.setEnableLoadMore(false);
                    }
                } else {
                    refreshLayout.setEnableLoadMore(false);
                }
            }
        });

        quickAdapter.add("");
        quickAdapter.add("");
        quickAdapter.add("");
        quickAdapter.add("");
        quickAdapter.add("");
        quickAdapter.add("");
        quickAdapter.add("");
        quickAdapter.add("");
        quickAdapter.add("");
        quickAdapter.notifyDataSetChanged();
    }

    @Override
    protected void initData() {

    }

    @Override
    @OnClick({R.id.dynamic_selesct_top, R.id.shop_selesct_top})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {
            case R.id.dynamic_selesct_top:
                break;
            case R.id.shop_selesct_top:
                break;
        }
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {

    }

}
